package com.edu.atams.service.impl;

import com.edu.atams.common.dto.YearAllStudentDto;
import com.edu.atams.entity.Grade;
import com.edu.atams.mapper.GradeMapper;
import com.edu.atams.service.GradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-10-09
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {
    @Autowired
    GradeMapper gradeMapper;

    @Override
    public List<YearAllStudentDto> getYearAllStudent() {
        List<YearAllStudentDto> data = gradeMapper.getYearAllStudent();
        Map<Integer, Integer> yearAllStudentMap = new HashMap<>();
        data.forEach(d -> yearAllStudentMap.merge(
                d.getYear(),
                d.getStudentNum(),
                Integer::sum
        ));
        data.clear();
        Iterator<Map.Entry<Integer, Integer>> iterator = yearAllStudentMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, Integer> next = iterator.next();
            YearAllStudentDto yearAllStudentDto = new YearAllStudentDto();
            yearAllStudentDto.setYear(next.getKey());
            yearAllStudentDto.setStudentNum(next.getValue());
            data.add(yearAllStudentDto);
        }
        return data;
    }
}
